<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="billCustomerId">
      <el-input v-model="dataForm.billCustomerId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="牌照id" prop="billLicenseId">
      <el-input v-model="dataForm.billLicenseId" placeholder="牌照id"></el-input>
    </el-form-item>
    <el-form-item label="停车场id" prop="billParkingId">
      <el-input v-model="dataForm.billParkingId" placeholder="停车场id"></el-input>
    </el-form-item>
    <el-form-item label="账单总计费用" prop="billFee">
      <el-input v-model="dataForm.billFee" placeholder="账单总计费用"></el-input>
    </el-form-item>
    <el-form-item label="开始时间" prop="billStart">
      <el-input v-model="dataForm.billStart" placeholder="开始时间" :disabled="true"></el-input>
    </el-form-item>
    <el-form-item label="结束时间" prop="billEnd">
      <el-input v-model="dataForm.billEnd" placeholder="结束时间" :disabled="true"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          billId: 0,
          billCustomerId: '',
          billLicenseId: '',
          billParkingId: '',
          billFee: '',
          billStart: '',
          billEnd: '',
          addTime: '',
          updateTime: '',
          deleted: ''
        },
        dataRule: {
          billCustomerId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          billLicenseId: [
            { required: true, message: '牌照id不能为空', trigger: 'blur' }
          ],
          billParkingId: [
            { required: true, message: '停车场id不能为空', trigger: 'blur' }
          ],
          billFee: [
            { required: true, message: '账单总计费用不能为空', trigger: 'blur' }
          ],
          billStart: [
            { required: true, message: '开始时间不能为空', trigger: 'blur' }
          ],
          billEnd: [
            { required: true, message: '结束时间不能为空', trigger: 'blur' }
          ],
          addTime: [
            { required: true, message: '添加时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          deleted: [
            { required: true, message: '逻辑删除不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.billId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.billId) {
			this.$axios.get(`/bill/info/${this.dataForm.billId}`).then((data)=>{
				if (data && data.data.code === 0) {
					this.dataForm.billCustomerId = data.data.bill.billCustomerId
					this.dataForm.billLicenseId = data.data.bill.billLicenseId
					this.dataForm.billParkingId = data.data.bill.billParkingId
					this.dataForm.billFee = data.data.bill.billFee
					this.dataForm.billStart = data.data.bill.billStart
					this.dataForm.billEnd = data.data.bill.billEnd
				}
			}).catch((error)=>{
				console.log(error)
			})
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
			var params = {
				'billId': this.dataForm.billId || undefined,
				'billCustomerId': this.dataForm.billCustomerId,
				'billLicenseId': this.dataForm.billLicenseId,
				'billParkingId': this.dataForm.billParkingId,
				'billFee': this.dataForm.billFee,
				'billStart': this.dataForm.billStart,
				'billEnd': this.dataForm.billEnd
			}
			this.$axios.post(`/bill/${!this.dataForm.billId ? 'save' : 'update'}`, params).then((data)=>{
				if (data && data.data.code === 0) {
				  this.$message({
				    message: '操作成功',
				    type: 'success',
				    duration: 1500,
				    onClose: () => {
				      this.visible = false
				      this.$emit('refreshDataList')
				    }
				  })
				} else {
				  this.$message.error(data.data.msg)
				}
			}).catch((error)=>{
				console.log(error)
			})
          }
        })
      }
    }
  }
</script>
